#!/bin/bash
echo "This will take some time, since it's cloning lots of code ..."
repo init -u https://github.com/OP-TEE/manifest.git -m qemu_v8.xml
repo sync -j4

echo "[*] patching toolchain.mk"
sed -i 's/11.3/12.2/g' build/toolchain.mk

echo "[*] patching TF-A makefiles"
echo "BL2_LDFLAGS += --no-warn-rwx-segments" >> trusted-firmware-a/bl2/bl2.mk
echo "BL31_LDFLAGS += --no-warn-rwx-segments" >> trusted-firmware-a/bl31/bl31.mk

echo "[*] patching OP-TEE configs"
sed -i 's/BR2_TOOLCHAIN_EXTERNAL_GCC_11/BR2_TOOLCHAIN_EXTERNAL_GCC_12/g' build/br-ext/configs/toolchain-aarch32
sed -i 's/BR2_TOOLCHAIN_EXTERNAL_GCC_11/BR2_TOOLCHAIN_EXTERNAL_GCC_12/g' build/br-ext/configs/toolchain-aarch64

echo "[*] patching optee_test configs"
sed -i '1s;^;WITH_CXX_TESTS := n\n;' build/common.mk

cd build
make -j2 toolchains
make -j8

# echo "Now do this:"
# echo "$ make -j8 run"
